Creating a Data Source
This section contains sample code that is provided as an example of using the DataDirect Connection Pool Manager to allow your applications to handle connection pooling.
Creating a DataDirect SequeLink® Data Source Object
The following example shows how to create a SequeLink for JDBC DataSource object and register it to a JNDI naming service. The DataSource class is provided by your SequeLink for JDBC driver and is database-independent. In the following example we use Oracle, so the DataSource class is SequeLinkDataSource. See "Developing JDBC Applications" for the name of the DataSource class.
If you want the client application to use non-pooled connections (see "Connecting to a Data Source"), you must modify this example so that the JNDI entry is registered using the name jdbc/SparkyOracle.
If you want the client application to use pooled connections, the JNDI entry must map to the DataSource of the DataDirect Connection Pool Manager. Therefore, you must register two data sources:
- The Connection Pool Manager's Data Source using the example in "Creating a Data Source Using the DataDirect® Connection Pool Manager". This process registers the data source using the JNDI entry jdbc/SparkyOracle. The Connection Pool Manager will create physical connections using the JNDI Entry jdbc/SequeLinkSparkyOracle.
- A SequeLink DataSource, using the following example to register the DataSource using the JNDI entry jdbc/SequeLinkSparkyOracle.
//************************************************************************ // // This code creates a SequeLink for JDBC data source and registers it to a // JNDI naming service. This SequeLink for JDBC data source uses the // DataSource implementation provided by the SequeLink for JDBC Driver. // // If you want users to use non-pooled connections, you must modify this // example so that it registers the SequeLink Data Source using the JNDI // entry <jdbc/SparkyOracle>. // // If you want users to use pooled connections, use this example as is // to register the SequeLink Data Source using the JNDI entry // <jdbc/SequeLinkSparkyOracle>. Also, use the example in the next section // to register the Connection Pool Manager's Data Source using the JNDI entry // <jdbc/SparkyOracle> // //************************************************************************ // From SequeLink for JDBC: import com.ddtek.jdbcx.sequelink.SequeLinkDataSource; import javax.sql.*; import java.sql.*; import javax.naming.*; import javax.naming.directory.*; import java.util.Hashtable; public class SequeLinkDataSourceRegisterJNDI { public static void main(String argv[]) { try { // Set up data source reference data for naming context: // ---------------------------------------------------- // Create a class instance that implements the interface // ConnectionPoolDataSource OracleDataSource ds = new SequeLinkDataSource(); ds.setDescription( "Oracle on Sparky - SequeLink Data Source"); ds.setServerName("sparky"); ds.setPortNumber(19996); ds.setUser("scott"); ds.setPassword("test"); // Set up environment for creating initial context Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource"); Context ctx = new InitialContext(env); // Register the data source to JNDI naming service ctx.bind("jdbc/SequeLinkSparkyOracle", ds); } catch (Exception e) { System.out.println(e); return; } } // Main } // class SequeLinkDataSourceRegisterJNDICreating a Data Source Using the DataDirect® Connection Pool Manager
The following Java code example creates a data source for JDBC and registers it to a JNDI naming service. The PooledConnectionDataSource class is provided by the DataDirect com.ddtek.pool package. In the following code example, the PooledConnectionDataSource object references a JDBC data source object. Therefore, the example performs a lookup by setting the DataSourceName attribute to the JNDI name of a registered pooled data source (in this example, jdbc/SequeLinkSparkyOracle, which is the JDBC DataSource object created in section "Creating a DataDirect SequeLink® Data Source Object").
Client applications that use this data source must perform a lookup using the registered JNDI name (jdbc/SparkyOracle in this example).
//************************************************************************ // // This code creates a data source and registers it to a JNDI naming // service. This data source uses the PooledConnectionDataSource // implementation provided by the DataDirect com.ddtek.pool package. // // This data source refers to a previously registered pooled data source. // // This data source registers its name as <jdbc/SparkyOracle>. // Client applications using pooling must perform a lookup for this name. // //************************************************************************ // From the DataDirect connection pooling package: import com.ddtek.pool.PooledConnectionDataSource; import javax.sql.*; import java.sql.*; import javax.naming.*; import javax.naming.directory.*; import java.util.Hashtable; public class PoolMgrDataSourceRegisterJNDI { public static void main(String argv[]) { try { // Set up data source reference data for naming context: // ---------------------------------------------------- // Create a pooling manager's class instance that implements // the interface DataSource PooledConnectionDataSource ds = new PooledConnectionDataSource(); ds.setDescription("Sparky Oracle - Oracle Data Source"); // Refer to a previously registered pooled data source to access // a ConnectionPoolDataSource object ds.setDataSourceName("jdbc/SequeLinkSparkyOracle"); // The pool manager will be initiated with 5 physical connections ds.setInitialPoolSize(5); // The pool maintenance thread will make sure that there are // at least 5 physical connections available ds.setMinPoolSize(5); // The pool maintenance thread will check that there are no more // than 10 physical connections available ds.setMaxPoolSize(10); // The pool maintenance thread will wake up and check the pool // every 20 seconds ds.setPropertyCycle(20); // The pool maintenance thread will remove physical connections // that are inactive for more than 300 seconds ds.setMaxIdleTime(300); // Set tracing off since we choose not to see output listing // of activities on a connection ds.setTracing(false); // Set up environment for creating initial context Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource"); Context ctx = new InitialContext(env); // Register the data source to JNDI naming service // for application to use ctx.bind("jdbc/SparkyOracle", ds); } catch (Exception e) { System.out.println(e); return; } } // Main } // class PoolMgrDataSourceRegisterJNDI